package com.jilujia.domain.user;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.user.repository.UserFollowHotelRepository;
import com.jilujia.framework.domain.AbstractPoEntityManager;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class UserFollowHotelManager extends AbstractPoEntityManager<Long, UserFollowHotel, UserFollowHotelRepository> {

	@Transactional
	public void follow(Long userId, Long hotelId) {
		UserFollowHotel userFollow = this.getRepository().findByUserIdAndHotelId(userId, hotelId);
		if (userFollow != null) {
			// do nothing
		} else {
			userFollow = new UserFollowHotel(userId, hotelId);
			this.setInstance(userFollow);
			this.save();
		}

	}

	@Transactional
	public void unfollow(Long userId, Long hotelId) {
		UserFollowHotel userFollow = this.getRepository().findByUserIdAndHotelId(userId, hotelId);
		if (userFollow != null) {
			this.setInstance(userFollow);
			this.remove();
		}
	}
}
